package com.jd.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.jd.po.User;
import com.jd.service.GetLoginUrlService;
import com.jd.service.GetProjectListService;
import com.jd.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginByAjaxAction extends ActionSupport {
	private String username;
	private String password;
	private String group;
	private String tOrb;
	private UserService userService;
	private GetLoginUrlService getLoginUrlService;
	
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String execute() throws Exception {
		User user = new User();
		User queryUser;
		String url = null;
		user.setUsername(getUsername());
		user.setPassword(getPassword());
		HttpServletResponse response = ServletActionContext.getResponse();
		//HttpServletRequest request = ServletActionContext.getRequest();
		Map session = ActionContext.getContext().getSession();
		queryUser = userService.loginUser(user);
		if(queryUser!=null) {
			queryUser.setUserGroup(getGroup());
			url = getGetLoginUrlService().getLoginUrl(queryUser, getTOrb());
			session.put("user", queryUser);
		}
		PrintWriter out=response.getWriter();
		System.out.println("目标服务器："+url);
		out.print(url);
		out.flush();
		return null;
		
	}
	
	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return password;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUsername() {
		return username;
	}

	public void setTOrb(String tOrb) {
		this.tOrb = tOrb;
	}

	public String getTOrb() {
		return tOrb;
	}

	public void setGetLoginUrlService(GetLoginUrlService getLoginUrlService) {
		this.getLoginUrlService = getLoginUrlService;
	}

	public GetLoginUrlService getGetLoginUrlService() {
		return getLoginUrlService;
	}

	public void setGroup(String group) {
		this.group = group;
	}

	public String getGroup() {
		return group;
	}


}
